package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.ChangeIsReadReq;
import sz.szsmk.citizencard.model.network.bean.request.NoticeDelItemReq;
import sz.szsmk.citizencard.model.network.bean.request.NoticeListDelReq;
import sz.szsmk.citizencard.model.network.bean.request.NoticeListReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.NoticeListRes;
import sz.szsmk.citizencard.mvp.contract.INoticeListContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * 欣网互联网络科技有限公司
 * author: zdh
 * created on: 2018/8/13 17:16
 * description:
 */
public class NoticeListPresenter extends IBasePresenter
        implements INoticeListContact.IINoticeListPresenter<INoticeListContact.INoticeListView> {
    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public NoticeListPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void getNoticeList(int currentPager, String word, String messageType) {
        NoticeListReq noticeListReq = new NoticeListReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        noticeListReq.setAccess_token(access_token);
        noticeListReq.setDevice_id("47");
        noticeListReq.setKeyWord(word);
        noticeListReq.setMessageType(messageType);
        noticeListReq.setPageNo(currentPager);
        noticeListReq.setPageSize(10);
        if (word.equals("")) {
            noticeListReq.setQueryType("1");
        } else {
            noticeListReq.setQueryType("2");
        }
        noticeListReq.setSign("123");
        mNetworkDateSource.getNotcieList(noticeListReq, new SubscribeResult<NoticeListRes>() {
            @Override
            protected void onOk(NoticeListRes response) {
//                String json = new Gson().toJson(response);
                List<NoticeListRes.MessageDetailListBean> detailList = response.getMessageDetailList();
                if (detailList != null) {
                    ((INoticeListContact.INoticeListView) mView).getNoticeListSuccess(detailList);
                }
            }

            @Override
            protected void onFailure(NoticeListRes response) {
                super.onFailure(response);
                ((INoticeListContact.INoticeListView) mView).getNoticeListError(response.getMessage());
            }

            @Override
            protected void onNext(NoticeListRes response) {
                super.onNext(response);
                ((INoticeListContact.INoticeListView) mView).getNoticeListError(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((INoticeListContact.INoticeListView) mView).getNoticeListError(throwable.getMessage());

            }
        });
    }

    @Override
    public void changesIsRead(String id, String type) {
        ChangeIsReadReq changeIsReadReq = new ChangeIsReadReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        changeIsReadReq.setAccess_token(access_token);
        changeIsReadReq.setDevice_id("adfaf");
        changeIsReadReq.setSign("adsfa");
        changeIsReadReq.setId(id);
        changeIsReadReq.setType(type);
        String json = new Gson().toJson(changeIsReadReq);
        L.e(Constant.TAG, "changeIsRead---->" + json);
        mNetworkDateSource.changesIsRead(changeIsReadReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {

            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void delItem(String id) {
        NoticeListDelReq noticeListDelReq = new NoticeListDelReq();
        noticeListDelReq.setId(id);
        noticeListDelReq.setType("2");
        noticeListDelReq.setSign("34");
        String json = new Gson().toJson(noticeListDelReq);
        L.e(Constant.MTAG, "noticeListDelreq" + json);
        mNetworkDateSource.deleteMessageByDetailId(noticeListDelReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {

            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void clearItem(String messageType) {
        NoticeDelItemReq noticeDelItemReq = new NoticeDelItemReq();
        noticeDelItemReq.setMessageType(messageType);
        String json = new Gson().toJson(noticeDelItemReq);
        L.e(Constant.MTAG, "消息删除条目---->" + json);
        mNetworkDateSource.deleteMessageTypeByUser(noticeDelItemReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                L.e(Constant.MTAG,"------1");
                ((INoticeListContact.INoticeListView) mView).clearItemSucc();
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                L.e(Constant.MTAG,"------2");
                ((INoticeListContact.INoticeListView) mView).clearItemFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                L.e(Constant.MTAG,"------3");
                ((INoticeListContact.INoticeListView) mView).clearItemFail();
            }
        });

    }
}
